package com.ch07.controller;

import com.ch07.service.PaymentService;
import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * @ClassName PaymentController
 * @Description TODO
 * @Author Kx
 * @Date 2025/4/29 10:54
 * @Version 1.0
 */
@Controller
public class PaymentController {
    /***
     * @author: kx
     * @date: 2025/4/29 10:55
     * @description: 注入一个map集合,spring容器会将此接口的所有实现类都注入到这个map中,key是bean的id,value是bean的实例
    */
    private Map<String, PaymentService> paymentServiceMap;

    /**
     * @author: kx
     * @date: 2025/4/29 10:57
     * @description: 自动注入map集合
    */
    public PaymentController(Map<String, PaymentService> paymentServiceMap) {
        this.paymentServiceMap = paymentServiceMap;
    }

    /**
     * @author: kx
     * @date: 2025/4/29 10:57
     * @param type : 支付方式
     * @description: 根据类型找出响应的支付策略实现来完成支付
    */
    public void payment(String type) {
        // 获取响应的支付策略实现
        PaymentService paymentService = paymentServiceMap.get(type);
        // 完成支付
        paymentService.payment();
    }
}
